<?php
/**
 * 图片裁剪公用类
 * Author：胡仟
 * Createdate：2012-06-07
 */

class ImageClip{
	private $source_img_src;//原图片的地址
	private $target_img_path;//目标图片的保存地址
	private $target_img_width;//目标图片的宽度
	private $target_img_height;//目标图片的宽度
	private $clip_img_width;//图片截取区域的宽度
	private $clip_img_height;//图片截取区域的宽度	
	private $x;//截取区域左上角距离左边的宽度
	private $y;//截取区域左上角距离上边的高度
	private $quality = 100;//图片质量
	private $target_img_name;//目标图片名称 
	
	function __construct($source_img_src,$target_img_path,$target_img_width,$target_img_height,$clip_img_width,$clip_img_height,$x,$y,$target_img_name){
		$this->source_img_src = $source_img_src;
		$this->target_img_path = $target_img_path;
		$this->target_img_width = $target_img_width;
		$this->target_img_height = $target_img_height;
		$this->clip_img_width = $clip_img_width;
		$this->clip_img_height = $clip_img_height;
		$this->x = $x;
		$this->y = $y;
		$this->target_img_name = $target_img_name;
	}
	
	/**
	 * 获取图片类型
	 * @return string
	 */
	function getExtend(){
		$img = $this->source_img_src;
		$pathinfo = pathinfo($img);
		$ext = strtolower($pathinfo['extension']);
		return $ext;	
	}
	
	/**
	 * 获取图片名称
	 * @return string
	 * @return unknown
	 */
	function getImageName(){
		$img = $this->source_img_src;
		$image_name = basename($img,'.'.$this->getExtend());
		return $image_name;
	}
	
	/**
	 * 获取图像标示符
	 * @param $ext 图像类型
	 * @return  bool
	 */
	function getCreateImg(){
		$ext = $this->getExtend();
		$src = $this->source_img_src;//原图像
        $pic_creat = '';
        switch($ext){
                case 'jpg':
                        $pic_creat = @imagecreatefromjpeg($src);
                        break;
                case 'jpeg':
                        $pic_creat = @imagecreatefromjpeg($src);
                        break;                        
                case 'gif':
                        $pic_creat = @imagecreatefromgif($src);
                        break;
                case 'png':
                        $pic_creat = @imagecreatefrompng($src);
                        break;
                /**        
                case 'bmp':
                        $pic_creat = @imagecreatefromwbmp($src);
                        break;
                **/        
                default:
                        return false;
                        break;
        }
        return 	$pic_creat;	
	}
	
	/**
	 * 创建空图像
	 * @return
	 */
	function createEmptyImage(){
		$target_img_width = $this->target_img_width;
		$target_img_height = $this->target_img_height;
		$new_pic = @imagecreatetruecolor($target_img_width,$target_img_height);
        if(!$new_pic){
        	return false;
        }else {
        	return $new_pic;
        }
	}
	
	/**
	 * 复制图像
	 * @return bool
	 */
	function copyImage($new_pic='',$pic_creat=''){
		if(empty($new_pic)) $new_pic = $this->createEmptyImage();
		if(empty($pic_creat)) $pic_creat = $this->getCreateImg();
		$re = @imagecopyresampled($new_pic,$pic_creat,0,0,$this->x,$this->y,$this->target_img_width,$this->target_img_height,$this->clip_img_width,$this->clip_img_height);
		if ($re){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 保存裁剪的图像
	 * @return bool
	 */
	function outImage($new_pic=''){
		if(empty($new_pic)) $new_pic = $this->createEmptyImage();
		$ext = $this->getExtend();
		$new_image = $this->target_img_path.$this->target_img_name.$ext;//截取图像保存的完整路径
		$src = $this->source_img_src;//原图像
        $out_file = '';
        switch($ext){
                case 'jpg':
                		$out_file = @imagejpeg($new_pic,$new_image,$this->quality);
                        break;
                case 'jpeg':
                        $out_file = @imagejpeg($new_pic,$new_image,$this->quality);
                        break;                        
                case 'gif':
                        $out_file = @imagegif($new_pic,$new_image,$this->quality);
                        break;
                case 'png':
                        $out_file = @imagepng($new_pic,$new_image,$this->quality);
                        break;
                default:
                        return false;
                        break;
        }
        if($out_file){
                return true;
        }else{
                return false;
        }		
	}
	
}

?>